/**
 * Created by steven on 2016/4/22.
 */
$(document).on('click', '.sd_li', function() {
//$(".sd_li").on('click',function(){
    if( $(this).find("span").eq(2).hasClass("glyphicon-menu-right")){
        $('.glyphicon-menu-down').addClass("glyphicon-menu-right");
        $('.glyphicon-menu-down').removeClass("glyphicon-menu-down");
        $(this).find("span").eq(2).removeClass("glyphicon-menu-right");
        $(this).find("span").eq(2).addClass("glyphicon glyphicon-menu-down");
        $('.detail').hide();
        $(this).find($('.detail')).show();
    }else {
        $(this).find("span").eq(2).removeClass("glyphicon-menu-down");
        $(this).find("span").eq(2).addClass("glyphicon glyphicon-menu-right");
        $(this).find($('.detail')).hide();
    }
});


$("#search_btn").on('tap',function() {
    if($("#search_key").val()=='BG'){
        $("input[name='bg']").val($('#search_val').val());
    }
    if($("#search_key").val()=='YG'){
        $("input[name='yg']").val($('#search_val').val());
    }
    if($("#search_key").val()=='SPZ'){
        $("input[name='spz']").val($('#search_val').val());
    }
    $("#court_ann_form").submit();
});

$("#search_btn").on('click',function() {

    $("#sd_ann_form").submit();
});

var total=$('#totalcount').val();
var pages=get_pages(total,15);
var curpage=2;
var flag=true;
window.onscroll = function () {

    //监听事件内容

    if(getScrollHeight() >= getWindowHeight() + getDocumentTop()-40){
        //当滚动条到底时,这里是触发内容
        //异步请求数据,局部刷新dom
        //ajax_function()
        if(!flag){
            return;
        }
        flag=false;
        if(!(curpage<pages+1)){
            flag=true;
            return;
        }
        $('#loadingInfo').show();
        loadMoreData();


    }
}

function get_pages(total,pagesize){
    var page=total%pagesize==0 ? total/pagesize : Math.ceil(total/pagesize);
    return page;
}

//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}

//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;    return scrollHeight;
}

function loadMoreData(){

    var cbfy=$('select[name="cbfy"]').val();
    var url='index.php/sdannounce/list';
    var dt={
        pageno:curpage,
        pagesize:15,
        cbfy:cbfy,
        isAjax:true
    };
    var aj = $.ajax( {
        url:url,// 跳转到 action
        data:dt,
        type:'post',
        cache:false,
        dataType:'json',
        success:function(data) {
            var isSuccess=data.isSuccess;
            if(!isSuccess){
                giveAlert(data.errorMessage);
            }else{
                renderhtml(data.data.list);
                $('#loadingInfo').hide();
                curpage+=1;
                flag=true;
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(XMLHttpRequest.status);
            //alert(XMLHttpRequest.readyState);
            //alert(textStatus);
            $('#loadingInfo').hide();
            curpage+=1;
            flag=true;
        }
    });
}

function renderhtml(obj){
    var str='';
    for(var i=0;i<obj.length;i++){
        var content=obj[i].Content;
        if (content.length>=22){
            content=content.substring(0,20);
        }
        var time=obj[i].Time;
        str+='<li  class="list-group-item sd_li">';
        str+='<div class="row" style="margin-bottom: 10px;">';
        str+='<div >';
        str+='<div class="col-xs-3">';
        str+='<span>案件号</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span>'+obj[i].CaseNo+'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div >';
        str+='<div class="col-xs-11">';
        str+=content;
        //<?php
        //if(mb_strlen($item->Content)<22){
        //    echo $item->Content;
        //}else{
        //    $outs=mb_substr($item->Content,0,20).'...';
        //    echo $outs;
        //}
        //?>
        str+='</div>';
        str+='<div style="text-align:center;">';
        str+='<span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>';
        str+='</div>';
        str+='</div>';
        str+='</div>';
        str+='<div class="row detail" style="display:none; border-top-style: solid;border-top-width: 1px;border-top-color: #888888" >';
        str+='<div style="text-align: center"><span ><strong>'+obj[i].CaseNo +'</strong></span></div>';
        str+='<div style="text-align: center"><span>发布时间:';
        str+=time+'</span>';
        str+='</div>';
        str+='<p>';
        str+=obj[i].Content;
        str+='</p>';
        str+='</div>';
        str+='</li>';

    }
    $('#sdul').append(str);
}